//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from "@tauri-apps/api/core";

export type MsgInfo = {
    msg_id: string;
    content: string;
    send_user_id: string;
    send_display_name: string;
    send_logo_uri: string;
    send_time: number;
};

export type SendRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    content: string;
};

export type SendResponse = {
    code: number;
    err_msg: string;
    msg_id: string;
};

export type ListAfterRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    msg_id: string;
    limit: number;
};

export type ListAfterResponse = {
    code: number;
    err_msg: string;
    msg_list: MsgInfo[];
    last_msg_id: string;
};

export type ListBeforeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    msg_id: string;
    limit: number;
};

export type ListBeforeResponse = {
    code: number;
    err_msg: string;
    msg_list: MsgInfo[];
    last_msg_id: string;
};

export type GetUnreadCountRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
};

export type GetUnreadCountResponse = {
    code: number;
    err_msg: string;
    unread_count: number;
};

export type ClearUnreadRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
};

export type ClearUnreadResponse = {
    code: number;
    err_msg: string;
};

// 发送消息
export async function send(request: SendRequest): Promise<SendResponse> {
    const cmd = 'board_chat_api_cmds_send';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<SendResponse>(cmd, {
        request,
    });
}

// 列出后续消息 
export async function list_after(request: ListAfterRequest): Promise<ListAfterResponse> {
    const cmd = 'board_chat_api_cmds_list_after';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListAfterResponse>(cmd, {
        request,
    });
}

// 列出前置消息
export async function list_before(request: ListBeforeRequest): Promise<ListBeforeResponse> {
    const cmd = 'board_chat_api_cmds_list_before';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListBeforeResponse>(cmd, {
        request,
    });
}

// 获取未读消息数量
export async function get_unread_count(request: GetUnreadCountRequest): Promise<GetUnreadCountResponse> {
    const cmd = 'board_chat_api_cmds_get_unread_count';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetUnreadCountResponse>(cmd, {
        request,
    });
}

// 清除未读消息数量
export async function clear_unread(request: ClearUnreadRequest): Promise<ClearUnreadResponse> {
    const cmd = 'board_chat_api_cmds_clear_unread';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ClearUnreadResponse>(cmd, {
        request,
    });
}